package net.zjitc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.zjitc.common.ResponseResult;
import net.zjitc.entity.Category;
import net.zjitc.exception.CategoryException;
import net.zjitc.service.ICategoryService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @auther 郭梦婷
 * @create 2022-04-27 14:23:08
 * @describe 前端控制器
 */
@CrossOrigin
@RestController
@Api(tags = "职位分类列表")
public class CategoryController {
    @Reference
    private ICategoryService categoryService;
    ResponseResult result=new ResponseResult();

    @ApiOperation("获取所有职位信息")
    @GetMapping("categories")
    public ResponseResult getCategories(@RequestParam(required = false) Integer type,
                                        @RequestParam(required = false) Integer pagenum,
                                        @RequestParam(required = false) Integer pagesize) throws CategoryException {
        try {
            if (pagenum==null||pagesize==null){
                List<Category> listCategory = categoryService.getListCategory(type);
                if (listCategory!=null){
                    result.setSuccess("获取成功",listCategory);

                }
            }else {
                Page<Category> pageCateGory = categoryService.getPageCateGory(type, pagenum, pagesize);
                if (pageCateGory.getTotal()!=0){
                    result.setSuccess("获取成功",pageCateGory);
                }
            }
        }catch (Exception e){
            result.setError("获取失败");
        }
        return result;
    }
    //添加
    @ApiOperation("添加职位")
    @PostMapping("categories")
    public ResponseResult addcategories(@RequestBody Category category){
        Category c = categoryService.addLevel(category);
        if (c!=null){
            result.setSuccess("创建成功",c);
        }else {
            result.setError("创建失败");
        }
        return result;
    }
    //根据id查询
    @ApiOperation("根据id查询职位")
    @GetMapping("categories/{id}")
    public ResponseResult findCategoryById(@PathVariable(value = "id") Integer cat_id){
        Category c=categoryService.findById(cat_id);
        if (c!=null){
            result.setSuccess("查询成功",c);
        }else {
            result.setError("查询失败");
        }
        return result;
    }
    //根据id修改
    @ApiOperation("修改职位")
    @PutMapping("categories")
    public ResponseResult updateCategory(@RequestBody Category category){
        Category category1=categoryService.updateByid(category);
        if (category1!=null){
            result.setSuccess("更新成功",category1);
        }else {
            result.setError("更新失败");
        }
        return result;
    }
    //根据id删除
    @ApiOperation("删除职位")
    @DeleteMapping("categories/{id}")
    public ResponseResult deleteCategories(@PathVariable(value = "id") Integer id){
        Integer i=categoryService.deleteById(id);
        if (i!=-1){
            result.setSuccess("删除成功",null);
        }else {
            result.setError("删除失败");
        }
        return result;
    }


}

